今天回顧六角的課程範例,重做一遍產品編輯 modal ,但目前只做了一半
<template>
<!-- Button trigger modal -->
<button
type="button"
class="btn btn-primary"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
按這邊
</button>
<!-- Modal -->
<div
class="modal fade"
id="exampleModal"
tabindex="-1"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div class="modal-dialog modal-xl modal-fullscreen-md-down">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">商品資訊</h5>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<div v-for="i in 3" :key="'img' + i" class="mb-3">
<label :for="'img' + i" class="form-label">圖片網址</label>
<input
type="text"
class="form-control"
:id="'img' + i"
placeholder="image url"
// 與當前的產品 url 雙向綁定
v-model="tempProduct.imageUrl[i]"
<img :src="tempProduct.imageUrl[i]" :alt="tempProduct.title" />
/>
</div>
</div>
</div>
</div>
<div class="modal-footer">
// 登入才能取得 api 的 Token
<button @click="login">login</button>
// 取得 Token 才能捉取後台的產品清單
<button @click="getProductList">getProductList</button>
// 取得後台產品清單才能從中選一個產品,利用該產品 id,取得該產品完整資訊
<button @click="getProduct">getProduct</button>
<button
type="button"
class="btn btn-secondary"
data-bs-dismiss="modal"
>
Close
</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from "axios";
export default {
name: "Modal",
data() {
return {
user: {
email: "----",
password: "----",
},
token: "",
uuid: "----",
id: "----",
// 取得指定產品完整資料後,將他暫存與此
tempProduct: {
id: "",
title: "",
category: "",
content: "",
description: "",
imageUrl: [],
enabled: false,
origin_price: 0,
price: 0,
unit: "",
options: {
message: "Hello World!",
},
},
};
},
methods: {
login() {
const api = "我的 api";
axios
.post(api, this.user)
.then((res) => {
const token = res.data.token;
const expired = res.data.expired;
// 將 token 與他的到期時間存到瀏覽器 cookie 裡
document.cookie = `loginToken = ${token}; expires = ${new Date(
expired * 1000
)};`;
})
.catch((err) => {
console.log(err);
});
},
getProductList() {
const api = `我的 api`;
// 先從瀏覽器 cookie 取得 token
this.token = document.cookie.replace(/(?:(?:^|.*;\s*)loginToken\s*=\s*([^;]*).*$)|^.*$/, "$1");
// https://github.com/axios/axios#global-axios-defaults
// 並且 header 按照後端 api 文件的規格要求填上 Bearer token 字樣
axios.defaults.headers.common.Authorization = `Bearer ${this.token}`;
axios
.get(api)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
},
getProduct() {
const api = `我的 api`;
// 先從瀏覽器 cookie 取得 token
this.token = document.cookie.replace(/(?:(?:^|.*;\s*)loginToken\s*=\s*([^;]*).*$)|^.*$/, "$1");
// https://github.com/axios/axios#global-axios-defaults
// 並且 header 按照後端 api 文件的規格要求填上 Bearer token 字樣
axios.defaults.headers.common.Authorization = `Bearer ${this.token}`;
axios
.get(api)
.then((res) => {
console.log(res);
this.tempProduct = res.data.data;
})
.catch((err) => {
console.log(err);
});
},
},
};
</script>
<style></style>
今天筆記到這,如果內容有出入,都在麻煩糾正了,謝謝您 (っಠ‿ಠ)っ